package com.itdianzi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itdianzi.dao.cargo.FactoryDao;
import com.itdianzi.domain.cargo.Factory;
import com.itdianzi.domain.cargo.FactoryExample;
import com.itdianzi.service.cargo.FactoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private FactoryDao factoryDao;

    @Override
    public void save(Factory factory) {
        if (factory.getId() == null || StringUtils.isEmpty(factory.getId())) {
            //设置ID
            factory.setId(UUID.randomUUID().toString());
        }
        //保存到数据库
        factoryDao.insertSelective(factory);
    }

    /**
     * 修改厂家信息
     *
     * @param factory
     */
    @Override
    public void update(Factory factory) {
        factoryDao.updateByPrimaryKeySelective(factory);
    }

    /**
     * 根据ID删除厂家
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        factoryDao.deleteByPrimaryKey(id);
    }

    /**
     * 根据ID查询厂家信息
     *
     * @param id
     * @return
     */
    @Override
    public Factory findById(String id) {
        return factoryDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Factory> findAll(FactoryExample example) {
        return factoryDao.selectByExample(example);
    }

    @Override
    public PageInfo findAll(Integer page, Integer size, FactoryExample example) {
        PageHelper.startPage(page, size);
        List<Factory> factoryList = factoryDao.selectByExample(example);

        System.out.println(factoryList);

        return new PageInfo(factoryList);
    }
}
